<?php

namespace Application\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;

use Application\Model\SejourTable;

class SitemapTable
{
    protected $table ='Sitemap';
    protected $tableName ='Sitemap';
    
    protected $tableGateway;
    protected $sejourTable;

    public function __construct(TableGateway $tableGateway, SejourTable $sejourTable)
    {
    	$this->tableGateway = $tableGateway;
        $this->sejourTable = $sejourTable;
    }
    
    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select(function(Select $select){
            $select->order('sejourId ASC');
        });
        
        return $resultSet;
    }

    public function fetchAllArray()
    {
        $resultSet = $this->fetchAll();
        $entities = array();
        
        foreach ($resultSet as $row) {
        	$obj = new Sitemap();
        	$obj->exchangeArray( (array) $row);
        	$entities[] = $obj;
        }
        
        return $entities;
    }

    public function toArray ($resultSet)
    {
        $entities = array();
        
        foreach ($resultSet as $row) {
            $obj = new Sitemap();
            $obj->exchangeArray( (array) $row);
            $entities[] = $obj;
        }
        
        return $entities;
    }

    public function getSitemap($sejourId)
    {
        $id = (int) $sejourId;

        $rowset = $this->tableGateway->select(array('sejourId' => $id));
        $row = $rowset->current();
        if (!$row) {
            return false;
        }
        return $row;
    }

    public function getSitemapBySejourid( $sejourId )
    {
        $id = (int) $sejourId;

        $rowset = $this->tableGateway->select(array('sejourId' => $id));
        $row = $rowset->current();
        if (!$row) {
            return null;
        }
        return $row;
    }

    public function saveSitemapSejour(Sitemap $Sitemap)
    {
        $data = (array) $Sitemap;
        
        $id = (int)$Sitemap->sejourId;
        
        if ($this->getSitemap($id)) {
            $this->tableGateway->update($data, array('sejourId' => $id));
        } else {
            $this->tableGateway->insert($data);
            
            $id = $this->tableGateway->lastInsertValue;
        }
        
        return $id;
    }

    public function supprimerEntreeSitemap( $sejourId )
    {
        $this->tableGateway->delete(array('sejourId' => $sejourId));
    }

    public function reinitialiserTable() {
        $this->tableGateway->delete(array('sejourId is not null'));
    }

    // Générer une entrée ds la table Sitemap à partir de l'id d'un séjour
    public function genererNouvelleEntreeSitemap( $sejourId ) {
        $urlSejour = $this->sejourTable->genererSejourUrl( $sejourId );

        $sitemapObject = $this->getSitemapBySejourid( $sejourId );

        if( $sitemapObject != null ) {

            $sitemapObject->urlSitemap = $urlSejour;
            
            $this->saveSitemapSejour( $sitemapObject );
        } else {

            $sitemapArray = array('sejourId'=> $sejourId, 'urlSitemap'=> $urlSejour );

            $sitemap = new Sitemap();
            $sitemap->exchangeArray( $sitemapArray );

            $this->saveSitemapSejour( $sitemap );
        }
    }
}